package com.zjl.controller;


import com.zjl.pojo.Emp;
import com.zjl.pojo.Result;
import com.zjl.service.EmpService;
import com.zjl.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * @ClassName LoginController
 * @Description TODO
 * @Author ZJL
 * @Date 2023/10/31 23:40
 * @Version 1.0
 **/
@Slf4j
@RestController
public class LoginController {
    @Autowired
    private EmpService empService;

    /**
     * 用户登录
     * @param emp
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody Emp emp){
        log.info("员工登录：{}",emp);
        Emp loginEmp = empService.login(emp);

        //判断 登录用户名是否存在
        //登录成功 生产令牌  下发令牌
        if(loginEmp!= null){
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("id",loginEmp.getId());
            hashMap.put("username",loginEmp.getUsername());
            hashMap.put("name",loginEmp.getName());

            //生成令牌
            String token = JwtUtils.createJwt(hashMap);
            log.info("令牌：{}",token);
            return Result.success(token);
        }
        return Result.error("用户名或密码错误");
    }
}
